package xyz.naoyu.servlets.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import xyz.naoyu.dao.BaseDao;
import xyz.naoyu.entity.Auth;
import xyz.naoyu.entity.Auth;
import xyz.naoyu.servlets.service.AuthService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yWieLoc
 * @date 2022/3/18 19:17
 */
@WebServlet("/api/auth")
public class AuthServlet extends BaseServlet implements AuthService {


    /**
     * 返回认证标识数组
     * @param req
     * @param resp
     */
    @Override
    public void show(HttpServletRequest req, HttpServletResponse resp) {

        BaseDao tools = new BaseDao();
        JSONObject options = new JSONObject();

        options.put("filter", JSON.parseObject("{ " +
                "AuthType: true" +
                "}"));
        options.put("direct",JSON.parseObject("{" +
                "EId: " + "'" + req.getParameter("UId") + "'" +
                "}"));
        options.put("method","=");

        String res = tools.searchData("auth",options);
        System.out.println(res);
        try {
            resp.getWriter().append(res);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void edit(HttpServletRequest req, HttpServletResponse resp) {
        BaseDao tools = new BaseDao();
        Auth auth = new Auth(req.getParameter("Id"));
        
        try {
            // 后端信息
            auth = (Auth) auth.getInfo(tools);


            // 新建
            if(auth.getCreatorId() == null) {
                auth = JSON.parseObject(req.getParameter("auth"),auth.getClass());

                // 设定系统信息
                auth.setCreatorId((String) req.getSession().getAttribute("Id"));
                auth.setCreateTime(String.valueOf(System.currentTimeMillis()));

                // 更新
            } else  {
                // 用户编辑，认证
                if(auth.getCreatorId() != req.getSession().getAttribute("Id") ) {
                    resp.getWriter().append(JSON.toJSONString(
                            new BackCode(false,"无权限",null)
                    ));
                    return;
                }
                // 加载参数
                auth = JSON.parseObject(req.getParameter("auth"),auth.getClass());
                auth.setType("auth");
            }


            // 传向后端
            if(auth.infoUpdate(tools) == 0) {
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(false,"更新失败",null)
                ));
                return;
            } else {
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(true,"更新成功",null)
                ));
                return;
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void del(HttpServletRequest req, HttpServletResponse resp) {
        BaseDao tools = new BaseDao();
        Auth auth = new Auth(req.getParameter("Id"));


        try {
            auth = (Auth) auth.getInfo(tools);
            if(auth.getCreatorId()!=null && auth.getCreatorId() == req.getSession().getAttribute("Id")) {
                // 拥有权限
                if(auth.delSelf(tools)) {
                    resp.getWriter().append(JSON.toJSONString(
                            new BackCode(true, null,null)
                    ));
                }else {
                    resp.getWriter().append(JSON.toJSONString(
                            new BackCode(false, "删除失败",null)
                    ));
                }
            } else {
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(false, "权限不足",null)
                ));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void list(HttpServletRequest req, HttpServletResponse resp) {
        AuthService.super.list(req, resp);
    }
}
